from flask import Flask, request, render_template
import module.utils.api as api
from flask_cors import CORS
import json
import yaml
from gevent import pywsgi
import base64

app = Flask(__name__)
CORS(app, resources=r'/*')

with open("confs/host.yaml", "r", encoding='UTF-8') as f:
    config = yaml.load(f, Loader=yaml.FullLoader)


# 提取单张证件信息
@app.route('/ocr', methods=["POST"])
def ocr():
    input_data = json.loads(request.data)
    image_base64 = input_data["image"]
    paper_name = input_data["name"]
    if "face_flag" in input_data.keys():
        face_flag = input_data["face_flag"]
        result = api.ocr_recog_one(paper_name, image_base64, face_flag)
    else:
        result = api.ocr_recog_one(paper_name, image_base64)
    return result


# 证件人脸注册，当照片另起一页时，单独调用此接口获取人脸特征向量。
@app.route('/register', methods=["POST"])
def register():
    image_base64 = json.loads(request.data)["image"]
    return api.face_register(image_base64)


# 现场人脸核验
@app.route('/face', methods=["POST"])
def face():
    input_data = json.loads(request.data)
    image_base64 = input_data["image"]
    feature = input_data["feature"]
    result = api.match_face(image_base64, feature)
    return result


# 现场车牌提取
@app.route('/car', methods=["POST"])
def car():
    image_base64 = json.loads(request.data)["image"]
    return api.car_plate_extract(image_base64)


if __name__ == '__main__':
    server = pywsgi.WSGIServer((config["host"], config["port"]), app)
    server.serve_forever()

